﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.attend.personal
{
    public partial class duty : ModulePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            int num5;
            DateTime time2;
            DateTime time3;
            DateTime time4;
            int num = 30;
            int num2 = 10;
            int num3 = 10;
            int num4 = 60;
            AttendParaDao dao = new AttendParaDao();
            dao.GetPara();
            num = dao.DutyIntervalBefore1;
            num2 = dao.DutyIntervalAfter1;
            num3 = dao.DutyIntervalBefore2;
            num4 = dao.DutyIntervalAfter2;
            DataTable userConfig = new AttendConfigDao().GetUserConfig(base.Conn, HttpContext.Current.User.Identity.Name);
            this.litDutyName.Text = userConfig.Rows[0]["DutyName"].ToString();
            string content = "规定时间之前 " + num.ToString() + " 分钟，之后 " + num2.ToString() + " 分钟起可进行上班登记，规定时间之前 " + num3.ToString() + " 分钟，之后 " + num4.ToString() + " 分钟起可进行下班登记";
            this.litPromat.Text = base.ShowMessage(content).Replace("180", "350");
            DataTable table2 = new DataTable();
            table2.Columns.Add("No", Type.GetType("System.Int32"));
            table2.Columns.Add("DutyType", Type.GetType("System.Int32"));
            table2.Columns.Add("DutyTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("BeginTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("EndTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("DutyId", Type.GetType("System.Int32"));
            table2.Columns.Add("RegisterTime", Type.GetType("System.DateTime"));
            table2.Columns.Add("RegisterFlag", Type.GetType("System.Int32"));
            table2.Columns.Add("IsRegister", Type.GetType("System.Boolean"));
            table2.Columns["IsRegister"].DefaultValue = false;
            if (userConfig.Rows[0]["DutyTime1"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType1"].ToString());
                time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime1"].ToString());
                if (num5 == 1)
                {
                    time3 = time2.AddMinutes((double)-num);
                    time4 = time2.AddMinutes((double)num2);
                }
                else
                {
                    time3 = time2.AddMinutes((double)-num3);
                    time4 = time2.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 1, num5, time2, time3, time4 });
            }
            if (userConfig.Rows[0]["DutyTime2"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType2"].ToString());
                time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime2"].ToString());
                if (num5 == 1)
                {
                    time3 = time2.AddMinutes((double)-num);
                    time4 = time2.AddMinutes((double)num2);
                }
                else
                {
                    time3 = time2.AddMinutes((double)-num3);
                    time4 = time2.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 2, num5, time2, time3, time4 });
            }
            if (userConfig.Rows[0]["DutyTime3"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType3"].ToString());
                time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime3"].ToString());
                if (num5 == 1)
                {
                    time3 = time2.AddMinutes((double)-num);
                    time4 = time2.AddMinutes((double)num2);
                }
                else
                {
                    time3 = time2.AddMinutes((double)-num3);
                    time4 = time2.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 3, num5, time2, time3, time4 });
            }
            if (userConfig.Rows[0]["DutyTime4"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType4"].ToString());
                time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime4"].ToString());
                if (num5 == 1)
                {
                    time3 = time2.AddMinutes((double)-num);
                    time4 = time2.AddMinutes((double)num2);
                }
                else
                {
                    time3 = time2.AddMinutes((double)-num3);
                    time4 = time2.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 4, num5, time2, time3, time4 });
            }
            if (userConfig.Rows[0]["DutyTime5"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType5"].ToString());
                time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime5"].ToString());
                if (num5 == 1)
                {
                    time3 = time2.AddMinutes((double)-num);
                    time4 = time2.AddMinutes((double)num2);
                }
                else
                {
                    time3 = time2.AddMinutes((double)-num3);
                    time4 = time2.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 5, num5, time2, time3, time4 });
            }
            if (userConfig.Rows[0]["DutyTime6"] != DBNull.Value)
            {
                num5 = Convert.ToInt32(userConfig.Rows[0]["DutyType6"].ToString());
                time2 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + userConfig.Rows[0]["DutyTime6"].ToString());
                if (num5 == 1)
                {
                    time3 = time2.AddMinutes((double)-num);
                    time4 = time2.AddMinutes((double)num2);
                }
                else
                {
                    time3 = time2.AddMinutes((double)-num3);
                    time4 = time2.AddMinutes((double)num4);
                }
                table2.Rows.Add(new object[] { 6, num5, time2, time3, time4 });
            }
            string commandText = "SELECT * FROM [dbo].[Attend_Duty] WHERE [UserId] = '" + base.User.Identity.Name + "' AND [RegisterTime] >='" + DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00' AND [RegisterTime] <= '" + DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59'";
            DataTable table3 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText).Tables[0];
            table3.TableName = "Attend_Duty";
            for (int i = 0; i < table2.Rows.Count; i++)
            {
                DataRow[] rowArray = table3.Select("RegisterType = " + table2.Rows[i]["DutyType"].ToString() + " AND RegisterTime >='" + table2.Rows[i]["BeginTime"].ToString() + "' AND RegisterTime <='" + table2.Rows[i]["EndTime"].ToString() + "'");
                if (rowArray.Length > 0)
                {
                    table2.Rows[i]["DutyId"] = Convert.ToInt32(rowArray[0]["DutyId"].ToString());
                    table2.Rows[i]["RegisterTime"] = Convert.ToDateTime(rowArray[0]["RegisterTime"].ToString());
                    table2.Rows[i]["RegisterFlag"] = Convert.ToInt32(rowArray[0]["RegisterFlag"].ToString());
                }
                else
                {
                    table2.Rows[i]["RegisterFlag"] = 0;
                    if ((Convert.ToDateTime(table2.Rows[i]["BeginTime"].ToString()) <= DateTime.Now) && (DateTime.Now <= Convert.ToDateTime(table2.Rows[i]["EndTime"].ToString())))
                    {
                        table2.Rows[i]["IsRegister"] = true;
                    }
                }
            }
            this.rptMy.DataSource = table2;
            this.rptMy.DataBind();
        }
 

    }
}
